import pymysql


HOST = "172.17.0.2"
USER = "root"
PWD = "123.com"
DB_NAME = "test"

db = pymysql.connect(HOST,USER,PWD,DB_NAME,use_unicode=True)
cursor = db.cursor()
sql="select * from user"
cursor.execute(sql)
data = cursor.fetchone()
print(data)
sql = """
create table employee (
    first_name varchar(30) not null,
    last_name varchar(30) not null,
    age int,
    gender char(1),
    income float
)
"""
#cursor.execute(sql)
sql = """
insert into employee
(first_name,last_name,age,gender,income)
values
("zhang","leo",18,'f',90000)
"""
try:
    cursor.execute(sql)
    db.commit()
except:
    db.rollback()

sql="select * from employee"
cursor.execute(sql)
data = cursor.fetchone()#fetch one 
print(data)
sql = "select name,age from user"
try:
    cursor.execute(sql)
    results = cursor.fetchall()
    for row in results:
        print(row)
        print(row[0])
except:
    print("ERROR!!")
sql = "update employee set income = 900000 "
try:
    cursor.execute(sql)
    db.commit()
except:
    db.rollback()
sql = "select * from employee"
cursor.execute(sql)
data  = cursor.fetchone()
print(data)
sql = "delete from employee "
try:
    cursor.execute(sql)
    db.commit()
except:
    db.rollback()
print("="*30)
sql = "select * from employee"
cursor.execute(sql)
data  = cursor.fetchone()
print(data)
db.close()
